
var CreateNewWindow = function(config) {

	// 将字段加入到form中，以利用form进行样式排列
	this.createForm = new Ext.FormPanel({
				labelWidth : 70,
				labelAlign : 'right',
				url : '',
				bodyStyle : 'padding:10px 10px 0',
				autoScroll : false,
				items : [new Ext.form.TextField({
									fieldLabel : '类别',
									name : 'catname',
									value : catname,
									readOnly : true ,
									width : 150
								}), new Ext.form.TextField({
									fieldLabel : '联系人姓名',
									name : 'uname',
									width : 150,
									maxLength : 32
								}), new Ext.form.TextField({
									fieldLabel : '座机电话',
									width : 150,
									name : 'phone'
								}), new Ext.form.TextField({
									fieldLabel : '移动电话',
									width : 150,
									name : 'mobile'
								}), new Ext.form.TextField({
									fieldLabel : '传真',
									width : 150,
									name : 'fax'
								}), new Ext.form.TextField({
									fieldLabel : 'EMAIL',
									width : 150,
									name : 'email'
								}), new Ext.form.TextArea({
									fieldLabel : '地址',
									width : 150,
									name : 'address',
									maxLength : 100
								}), new Ext.form.TextField({
									hidden : true,
									width : 10,
									value : catid,
									name : 'catid'
								}), new Ext.form.TextField({
									hidden : true,
									width : 10,
									name : 'id'
								})]
			});

	Ext.apply(this, config);
	CreateNewWindow.superclass.constructor.call(this, {
				id : 'createNewWindow',
				closeAction : 'hide',
				layout : 'fit',
				width : 300,
				height : 350,
				modal : true,
				title : '联系人',
				items : this.createForm,
				buttons : [{
							text : '确定',
							handler : function(button, e) {
								if (this.actionType == "addNew") {
									this.doCreate(e);
								}else{
									this.doUpdate(e);
								}
								
							},
							scope : this
						}, {
							text : '取消',
							handler : function(button, e) {
								this.resetInput();
								this.fireEvent('cancelClicked', this, e);
								if (this.cancelHandler) {
									this.cancelHandler.call(this.scope || this,
											this, e);
								} else {
									this.hide();
								}
							},
							scope : this
						}]
			});
};

Ext.extend(CreateNewWindow, Ext.Window, {
	/** 操作类型 */
	actionType : null,
	/** 新建表单 */
	createForm : null,
	/** 确定按钮事件处理器 */
	okHandler : null,
	/** 取消按钮事件处理器 */
	cancelHandler : null,
	/** 事件触发时对象的工作域 */
	scope : null,

	/**
	 * 显示窗口
	 */
	showWindow : function( actionType, id) {
		this.actionType = actionType;
		this.show();
		var form = this.createForm.getForm();
		if (actionType == "edit") {
			form.load({
				url : context + 'addressbook.do?method=editAddrBook',
				params : {
					id : id
				},
				waitTitle : '提示',
				waitMsg : '正在加载数据,请稍候...',
				animEl : "loding",
				success : function(form, action) {
				
				},
				failure : function(form, action) {
					Ext.getCmp('updateobjcorebtn').disable();
					
					if (action.result.errCode && action.result.errCode == '-1') {
						Ext.Msg.alert('读取属性信息失败,未找到对象结构');
					} else if (action.result.errCode
							&& action.result.errCode == '-2') {
						Ext.Msg.alert('读取属性信息失败,权限不足');
					} else if (action.result.errCode
							&& action.result.errCode == '-3') {
						Ext.Msg.alert('读取属性信息失败');
					} else {
						Ext.Msg.alert('未知的异常错误');
					}
				}
			});
		} else {

			form.reset();
		}

	},

	/**
	 * 将输入框清空
	 */
	resetInput : function() {
		var form = this.createForm.getForm();
		form.reset();
	},

	/**
	 * 新建
	 */
	doCreate : function(event) {
		var form = this.createForm.getForm();

		if (form.isValid()) {
			form.submit({
				url : context + "/app/addressbook.do?method=addAddrBook",
				waitMsg : '保存中...',
				method : 'POST',/**/
				params : {
					userId : userId
				},
				scope : this,
				success : function(form, action) {

					this.resetInput();
					// 执行确定处理
					this.fireEvent('okClicked', this);
					if (this.okHandler) {
						this.okHandler.call(this.scope || this, this, event);
					} else {
						this.hide();
					}
					var url = context + "/app/addressbook.do?method=query&catid=" + catid;
					document.form1.action = url;
					document.form1.submit();
	

				},
				failure : function(form, action) {
					if (action.failureType === Ext.form.Action.CONNECT_FAILURE) {
						Ext.Msg.alert('保存失败', 'Status:'
										+ action.response.status + ': '
										+ action.response.statusText);
					}
					if (action.failureType === Ext.form.Action.SERVER_INVALID) {
						// server responded with success = false
						Ext.Msg.alert('保存失败', action.result.errors.errormsg);
					}

				}
			});

		} else {
			Ext.MessageBox.alert('错误', '请修正非法数据后再保存！');
			return;
		}
	},
	
	
	/**
	 * 更新
	 */
	doUpdate : function(event) {
		var form = this.createForm.getForm();

		if (form.isValid()) {
			form.submit({
				url : context + "/app/addressbook.do?method=updateAddrBook",
				waitMsg : '保存中...',
				method : 'POST',/**/
				scope : this,
				success : function(form, action) {

					this.resetInput();
					// 执行确定处理
					this.fireEvent('okClicked', this);
					if (this.okHandler) {
						this.okHandler.call(this.scope || this, this, event);
					} else {
						this.hide();
					}
					var url = context + "/app/addressbook.do?method=query&catid=" + catid;
					document.form1.action = url;
					document.form1.submit();
	

				},
				failure : function(form, action) {
					if (action.failureType === Ext.form.Action.CONNECT_FAILURE) {
						Ext.Msg.alert('保存失败', 'Status:'
										+ action.response.status + ': '
										+ action.response.statusText);
					}
					if (action.failureType === Ext.form.Action.SERVER_INVALID) {
						// server responded with success = false
						Ext.Msg.alert('保存失败', action.result.errors.errormsg);
					}

				}
			});

		} else {
			Ext.MessageBox.alert('错误', '请修正非法数据后再保存！');
			return;
		}
	},

	// private
	initComponent : function() {
		CreateNewWindow.superclass.initComponent.call(this);
		this.addEvents(
				/**
				 * @event okClicked 当确定按钮被点击时触发
				 * @param {CreateNewWindow}
				 *            this
				 */
				'okClicked',
				/**
				 * @event okClicked 当取消按钮被点击时触发
				 * @param {CreateNewWindow}
				 *            this
				 */
				'cancelClicked');
	}

});
var createNewWindow ; 
function handleInsert(catid, catname) {

	if (typeof(CreateNewWindow) == "undefined" || createNewWindow ==null ) {
		createNewWindow = new CreateNewWindow();
		}
		createNewWindow.showWindow('addNew',null);
	

}




function handleEdit(catid, catname, id) {
	if (typeof(CreateNewWindow) == "undefined" || createNewWindow ==null ) {
		createNewWindow = new CreateNewWindow();
		}
		createNewWindow.showWindow('edit',id);
}

var chk = false;
  function checkall(thizz){
    var chks = document.getElementsByTagName("input");
    for(var i=0;i<chks.length;i++){
      if(chks[i].type == "checkbox"){
        chks[i].checked = !chk;
      }
    }
    
    chk = !chk;
  }
  
  function handleDelete(){
    var rnt = false;
    var ids = "";
    var chks = document.getElementsByTagName("input");
    for(var i=0;i<chks.length;i++){
      if(chks[i].type == "checkbox" && chks[i].checked){
        rnt = true;
        ids += chks[i].value + ",";
      }
    }
    
    if(!rnt){
      alert("请选择要删除的数据");
    }else if(confirm("确定删除")){
      createHidden("ids",ids.substring(0,ids.length - 1));
      document.form1.action =context +  " /app/addressbook.do?method=deleteAddrBook";
      document.form1.submit();
    }
  }

function handleQuery() {
	var url = context + "/app/addressbook.do?method=query";

	document.form1.action = url;
	document.form1.submit();
}